package domain;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;

import dal.ConnectionManager;

public class Project {

	/** id of project */
	private Integer id;
	/** name of project */
	private String name;
	/** description of project */
	private String desc;
	/** starting date of project */
	private Date startDate = null;
	/** id of the project manager of project */
	private int personId;
	/** This is used to converting the date yyyy-MM-dd format */
	SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
	/** These instances are needed for database connection */
	ConnectionManager mgr = ConnectionManager.getInstance();
	Connection con = mgr.getConnection();
	

	
	/** Empty constructor of the Project class */
	public Project() {
		super();
	}

	/**
	 * Constructor of the Project class
	 *                           
	 * @param name Name of the project
	 * @param desc Description of the project
	 * @param dateInput Starting date of the project
	 * @param personId Id of the person who is the project manager of the project 
	 */
	public Project(String name, String desc, String dateInput, Integer personId) {
		super();
		this.name = name;
		this.desc = desc;
		this.personId = personId;
		setStartDate(dateInput);
		
	}
	
	/**
	 * Create a new Project
	 * @param myProject Object of project
	 * @param myActivity Object of activity
	 * @param myTask Object of Task
	 * @return
	 */
	public boolean create(Project myProject, Activity myActivity, Task myTask)
	{
		if(myProject.selectById(myProject.getId())==null) 
	   {
		myProject.addProject(myProject);
	   }
		if(myActivity.create(myProject, myActivity, myTask))
		{
		return true;
		}
		else{
		System.out.println("PLEASE ENTER THE VALID STARTDATES AND TRY AGAIN!!!");
		return false;
		}
	   
	}
	
	/**
	 * Adding a project to database
	 * @param myProject
	 */
	public void addProject(Project myProject)
    {
    	
		try {
			Statement st = con.createStatement();
			st.executeUpdate("insert into project (name, description, startDate, personId) values ('"+myProject.getName()+"', '"+myProject.getDesc()+"', '"+myProject.getDateToString()+"', "+ myProject.getPersonId()+")");
			ResultSet rs = st.executeQuery("select id from project where name='"+ myProject.getName() +"' and description='"+ myProject.getDesc() +"' and startDate='"+ myProject.getDateToString()+"'");
			while (rs.next()) {
				myProject.setId(rs.getInt(1));	
			}		
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	
    }

	/**
	 * Removing a project from database
	 * @param myProject
	 */
	public void removeProject(Project myProject)
    {
		try {
			Statement st = con.createStatement();
			st.executeUpdate("delete from project where id="+ myProject.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
    }
	
	/**
	 * Updating a project 
	 * @param myProject
	 */
	public void updateProject(Project myProject)
	{
	    try {
			Statement st = con.createStatement();
			st.executeUpdate("update project set name='"+ myProject.getName() +"', description='"+ myProject.getDesc() +"', startDate='"+ myProject.getDateToString()+"' where id="+myProject.getId());
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	
	/**
	 * Selecting all projects in the system
	 * @return ArrayList<Project>
	 */
	public ArrayList<Project> selectAllProject() 
	{
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from project");
			ArrayList<Project> myProjectList = new ArrayList<Project>();
			
			while (rs.next()) {
					 Project myProject = new Project();
			    	 myProject.setId(rs.getInt(1));
					 myProject.setName(rs.getString(2));
					 myProject.setDesc(rs.getString(3));
					 myProject.setStartDate(rs.getString(4));
					 myProject.setPersonId(rs.getInt(5));
					 myProjectList.add(myProject);
			       } 
			return myProjectList;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Printing all projects in the system on console
	 */
	public void writeAllProject() 
	{
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from project");
			while(rs.next())
			{
			   System.out.println(rs.getInt(1)+" "+rs.getString(2)+" "+rs.getString(3)+" "+rs.getString(4)+ " " + rs.getInt(5));	
			}
						
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			
		}
	}
	
	/**
	 * Finding the project that given its id 
	 * @param id Project's id
	 * @return Project
	 */
	public Project selectById(Integer id) 
	{
		try {
			Statement st = con.createStatement();
			ResultSet rs = st.executeQuery("select * from project where id="+id);	
			Project myProject = new Project();
			if (rs.next()) {
			     do {
			    	 myProject.setId(rs.getInt(1));
					 myProject.setName(rs.getString(2));
					 myProject.setDesc(rs.getString(3));
					 myProject.setStartDate(rs.getString(4));
					 myProject.setPersonId(rs.getInt(5));
			       } while (rs.next());
			 	return myProject;  
			     }
			  else {
			    return null;
		}			
			
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return null;
		}
	}
	
	/**
	 * Getters and setters
	 */
	public Integer getId() {
		return id;
	}

	public void setId(Integer id) {
		this.id = id;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getDesc() {
		return desc;
	}

	public void setDesc(String desc) {
		this.desc = desc;
	}

	public Date getStartDate() {
		return startDate;
	}
	
	public String getDateToString()
	{
		return (format.format(startDate));
	}
	
	public void setStartDate(String dateInput) {
		
		try {
		      startDate = format.parse(dateInput);
		    } catch (ParseException e) {
		      e.printStackTrace();
		    }
	}
	public void setPersonId(int personId) {
		this.personId = personId;
	}

	public int getPersonId() {
		return personId;
	}
	
	

}
